
var gulp = require('gulp'),
    uglify = require('gulp-uglify');
    concat = require('gulp-concat');
    cssmin = require('gulp-clean-css');
	htmlmin = require('gulp-htmlmin');
	// imagemin = require('gulp-imagemin');
	sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var autoprefixer = require('gulp-autoprefixer');
	// sass = require('gulp-ruby-sass');
var browserSync = require('browser-sync').create();
var reload      = browserSync.reload;
// 简单的静态服务器
gulp.task('browser-sync', function() {
    browserSync.init({
        server: {
            baseDir: "./develop"
        },
        files: ["develop/**/*.css", "develop/**/*.html"]
    });
});
// 代理
// gulp.task('browser-sync', function() {
//     browserSync.init({
//         proxy: "你的域名或IP"
//     });
// });
gulp.task('mysass', function () {
    return gulp.src('develop/scss/**/*.scss')
        .pipe(sourcemaps.init())
        .pipe(sass({

            errLogToConsole: true,
            outputStyle: 'expanded',
            lineComments:true

        }).on('error', sass.logError))
        .pipe(sourcemaps.write('./'))
        // .pipe(autoprefixer())
        .pipe(gulp.dest('develop/css'))
});
gulp.task('watch-sass', function () {
    gulp.watch("develop/scss/**/*.scss", ["mysass"]);
});
//结合了browser-sync和sass的监听，可以通过改变sass文件的方法来直接看到效果
gulp.task('writeInSass',['browser-sync','watch-sass']);




gulp.task('watchsass2', function () {
    gulp.watch("sass/**/*.scss", ["testsass2"]);
});
gulp.task('testsass2', function () {
    return gulp.src('sass/**/*.scss')
        .pipe(sass())
        .pipe(gulp.dest('dist2/scss-css2'))
});


gulp.task('jsmin', function () {
    gulp.src('scripts/management.js')
        .pipe(uglify({
            mangle: {except: ['require' ,'exports' ,'module' ,'$']},//排除混淆关键字
            compress: true,//类型：Boolean 默认：true 是否完全压缩
            preserveComments: 'license' //保留所有注释
        }))
        .pipe(gulp.dest('dist/js'));
});
gulp.task('concatFiles', function () {
    gulp.src('scripts/*.js')
        .pipe(concat('all.js'))//合并后的文件名
        .pipe(gulp.dest('dist/concat-js'));
});
gulp.task('testCssmin', function () {
    gulp.src('css/*.css')
        .pipe(cssmin())
        .pipe(gulp.dest('dist/css'));
});


 
gulp.task('testHtmlmin', function () {
    var options = {
        removeComments: true,//清除HTML注释
        collapseWhitespace: true,//压缩HTML
        collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
        removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
        removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
        removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
        minifyJS: true,//压缩页面JS
        minifyCSS: true//压缩页面CSS
    };
    gulp.src('html/*.html')
        .pipe(htmlmin(options))
        .pipe(gulp.dest('dist/html'));
});

 
gulp.task('testImagemin', function () {
    gulp.src('img/*.{png,jpg,gif,ico}')
        .pipe(imagemin())
        .pipe(gulp.dest('dist/img'));
});





